package com.sws.orderMS.command.impl;

import java.util.ArrayList;
import java.util.List;

import com.sws.orderMS.OrderMSDao;
import com.sws.orderMS.command.UserCommand;
import com.sws.orderMS.pojo.Product;
import com.sws.orderMS.pojo.User;
import com.sws.orderMS.tool.Tool;

public class ListProductCommand extends UserCommand {

	public ListProductCommand(User userProfile) {
		super(userProfile);
	}

	@Override
	public void execute() {
		String mode = Tool.askUserInputNotNull("选择你要列出的商品方式(ALL|NAME|TYPE|CODE): ");
		
		if (!"ALL|NAME|TYPE|CODE".contains(mode.toUpperCase())) {
			Tool.printMsg("Invalid mode!");
			return;
		}
		
		OrderMSDao dao = new OrderMSDao();
		List<Product> products = new ArrayList<>();
		if ("ALL".equalsIgnoreCase(mode)) {
			products = dao.searchAllProducts();
		} else if ("NAME".equalsIgnoreCase(mode)) {
			String name = Tool.askUserInputNotNull("请输入商品名称:");
			products = dao.searchProductsByName(name);
		} else if ("TYPE".equalsIgnoreCase(mode)) {
			List<String> typeList = dao.getAllProductType();
			String selectMenu = "可供选择类型(";
			for (String str: typeList) {
				selectMenu = selectMenu.concat(str).concat("|");
			}
			selectMenu = selectMenu.concat(")");
			String type = Tool.askUserInputNotNull(selectMenu + ",请输入商品类型:");
			products = dao.searchProductsByType(type);
		} else if ("CODE".equalsIgnoreCase(mode)) {
			String code = Tool.askUserInputNotNull("请输入商品代号: ");
			Product product = dao.searchProductByCode(code);
			if (product == null) {
				Tool.printMsg("该商品代号不存在！");
				return;
			}
			Tool.printMsg(product.toString());
			return;
		}
		if (products.isEmpty()) {
			Tool.printMsg("没有找到你要的商品");
			return;
		}
		showProducts(products);
	}

	private void showProducts(List<Product> products) {
		for (Product p: products) {
			Tool.printMsg(p.toString());
		}
	}

}
